package com.moore.Servlet;

import com.moore.model.User;
import com.moore.net.HttpRequest;
import com.moore.net.HttpResponse;
import com.moore.net.HttpServlet;
import com.moore.utils.UserUtils;

import java.nio.charset.StandardCharsets;

public class LoginServlet extends HttpServlet {
    @Override
    public byte[] doGet(HttpRequest request, HttpResponse response) {
        String responseData;
        // 获取请求的数据
        String account = request.getParams().get("account");
        String password = request.getParams().get("password");

        if (account == null || password == null) {
            // 账号密码为空
            responseData = "empty";
        } else if (UserUtils.getUser(account, password) == null && UserUtils.getAdmin(account, password) == null) {
            // 账号密码错误
            responseData = "error";
        } else {
            // 账号密码正确
            if (UserUtils.getUser(account, password) == null){
                responseData = "Admin";
            } else {
                if (UserUtils.isBan(account).equals("success")){
                    responseData = User.getName();
                } else {
                    responseData = "ban";
                }
            }
        }
        return responseData.getBytes(StandardCharsets.UTF_8);
    }
}
